package com.gap.historyemployee;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Bynga;
import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.Template;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.entities.MasterUnit;
import com.buanalintas.bynga.formatter.DateFormatter;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.bynga.formatter.NumberFormatter;
import com.buanalintas.bynga.generator.PageList;
import com.gap.entities.GapMasterEmployee;
import com.gap.entities.GapTransactionHistoryEmployee;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;

/**
 *
 * Class HistoryEmployee is ...
 *
 * Generated on Fri Jan 09 13:17:48 ICT 2015
 *
 * @version 1.0
 * @author Mr. Kazao™
 * @since Bynga 1.0
 *
 */
public class HistoryEmployee extends Page {

    private Field historyId;
    private Field historyEmployeeName;
    private Field historyStatus;
    private Field historyType;
    private Field historyNumber;
    private Field historyDate;
    private Field historyPlatform;
    private Field historyValue;
    private Field historyCurrent;
    private Field historyLast;
    private Field historyDescription;
    private Field historyTimestamp;
    private Field historyUserId;
    private Field historyUnitId;
    private Field historyEmployeeId;
    private static String MESSAGE_DAILY = "<!--MESSAGE-DAILY-->";
    private static String MESSAGE_WEEKLY = "<!--MESSAGE-WEEKLY-->";
    private static String MESSAGE_MONTHLY_YEAR = "<!--MESSAGE-MONTHLY-YEAR-->";
    private static String MESSAGE_MONTHLY_MONTH = "<!--MESSAGE-MONTHLY-MONTH-->";
    private static String MESSAGE_YEARLY = "<!--MESSAGE-YEARLY-->";
    private static String MESSAGE_CUSTOM = "<!--MESSAGE-CUSTOM-->";
    private static String SELECT_MODE = "<!--SELECT-MODE-->";
    private static String SELECT_MODE_ONCHANGE = "<!--SELECT-MODE-ONCHANGE-->";
    private static String SELECT_VALUES = "<!--SELECT-VALUES-->";
    private static String SELECT_FILTER = "<!--SELECT-FILTER-->";
    private static String SELECT_FILTER_VALUES = "<!--SELECT-FILTER-VALUES-->";
    private static String MODE_DAILY = "MODE-DAILY";
    private static String DAILY_SELECT_DATE = "<!--DAILY-SELECT-DATE-->";
    private static String DAILY_DATE_VALUE = "<!--DAILY-DATE-VALUE-->";
    private static String MODE_WEEKLY = "MODE-WEEKLY";
    private static String WEEKLY_START_DATE_VALUE = "<!--WEEKLY-START-DATE-VALUE-->";
    private static String WEEKLY_SELECT_START_DATE = "<!--WEEKLY-SELECT-START-DATE-->";
    private static String MODE_MONTHLY = "MODE-MONTHLY";
    private static String MONTHLY_SELECT_YEAR = "<!--MONTHLY-SELECT-YEAR-->";
    private static String MONTHLY_SELECT_YEAR_VALUES = "<!--MONTHLY-SELECT-YEAR-VALUES-->";
    private static String MONTHLY_SELECT_MONTH = "<!--MONTHLY-SELECT-MONTH-->";
    private static String MONTHLY_SELECT_MONTH_VALUES = "<!--MONTHLY-SELECT-MONTH-VALUES-->";
    private static String MODE_YEARLY = "MODE-YEARLY";
    private static String YEARLY_SELECT_YEAR = "<!--YEARLY-SELECT-YEAR-->";
    private static String YEARLY_SELECT_YEAR_VALUES = "<!--YEARLY-SELECT-YEAR-VALUES-->";
    private static String MODE_CUSTOM = "MODE-CUSTOM";
    private static String CUSTOM_SELECT_START_DATE = "<!--CUSTOM-SELECT-START-DATE-->";
    private static String CUSTOM_START_DATE_VALUE = "<!--CUSTOM-START-DATE-VALUE-->";
    private static String CUSTOM_SELECT_END_DATE = "<!--CUSTOM-SELECT-END-DATE-->";
    private static String CUSTOM_END_DATE_VALUE = "<!--CUSTOM-END-DATE-VALUE-->";
    private static String BUTTON_SEARCH = "<!--BUTTON-SEARCH-->";
    private static String LIST_DATA = "<!--LIST-DATA-->";
    private static String RECORD = "<!--RECORD-->";
    private static String WARNING = "<!--WARNING-->";
    private static String TOTAL = "TOTAL";
    private static String TOTAL_CAPTION = "<!--TOTAL-CAPTION-->";
    private static String TOTAL_TEXT = "<!--TOTAL-TEXT-->";
    private static String RECAP_EXPORT_LINK = "<!--RECAP-EXPORT-LINK-->";
    private static String RECAP_EXPORT = "RECAP-EXPORT";
    private static String SELECT_EMPLOYEE = "<!--SELECT-EMPLOYEE-->";
    private static String SELECT_EMPLOYEE_VALUES = "<!--SELECT-EMPLOYEE-VALUES-->";
    private static String SELECT_UNIT = " <!--SELECT-UNIT-->";
    private static String SELECT_UNIT_VALUES = " <!--SELECT-UNIT-VALUES-->";
    private Action actionExport = new Action("export", "Export");

    public HistoryEmployee() {
        super();

        setTheClass(GapTransactionHistoryEmployee.class);




        historyId = new Field();
        historyId.setName("historyId");
        historyId.setCaption("id");
        //historyId.setInfo("history id");
        historyId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        historyId.setInputProperties(" size=\"20\"");
        getFields().add(historyId);

        historyEmployeeName = new Field();
        historyEmployeeName.setName("historyEmployeeName");
        historyEmployeeName.setCaption("name");
        //historyEmployeeName.setInfo("history employee name");
        historyEmployeeName.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        historyEmployeeName.setInputProperties(" size=\"20\"");
        getFields().add(historyEmployeeName);

        historyStatus = new Field();
        historyStatus.setName("historyStatus");
        historyStatus.setCaption("status");
        //historyStatus.setInfo("history platform");
        historyStatus.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        historyStatus.setInputProperties(" size=\"20\"");
        historyStatus.setInputType(Field.INPUT_CHECK_BOX);
        historyStatus.getOptions().add(new String[]{"Cancel", "Cancel"});
        historyStatus.getOptions().add(new String[]{"Revision", "Revision"});
        historyStatus.getOptions().add(new String[]{"Done", "Done"});
        getFields().add(historyStatus);

        historyNumber = new Field();
        historyNumber.setName("historyNumber");
        historyNumber.setCaption("Number");
        //historyEmployeeName.setInfo("history employee name");
        historyNumber.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        historyNumber.setInputProperties(" size=\"20\"");
        getFields().add(historyNumber);

        historyDate = new Field();
        historyDate.setName("historyDate");
        historyDate.setCaption("date");
        //historyEmployeeName.setInfo("history employee name");
        historyDate.setListProperties(" align=\"center\" width=\"1%\" nowrap");
        historyDate.setInputProperties(" size=\"20\"");
        getFields().add(historyDate);


        historyType = new Field();
        historyType.setName("historyType");
        historyType.setCaption("type");
        historyType.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        historyType.setInputProperties(" ");
        historyType.getOptions().add(new String[]{"1", "Transaction Employee"});
        historyType.getOptions().add(new String[]{"2", "Revision"});
        historyType.setInputType(Field.INPUT_COMBO_BOX);
        getFields().add(historyType);

        historyPlatform = new Field();
        historyPlatform.setName("historyPlatform");
        historyPlatform.setCaption("platform");
        historyPlatform.setFormater(MoneyFormatter.getInstance());
        historyPlatform.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        historyPlatform.setInputProperties(" size=\"20\"");
        getFields().add(historyPlatform);

        historyValue = new Field();
        historyValue.setName("historyValue");
        historyValue.setCaption(" value");
        historyValue.setFormater(MoneyFormatter.getInstance());
        historyValue.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        historyValue.setInputProperties(" size=\"20\"");
        getFields().add(historyValue);

        historyLast = new Field();
        historyLast.setName("historyLast");
        historyLast.setCaption("last");
        historyLast.setFormater(MoneyFormatter.getInstance());
        historyLast.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        historyLast.setInputProperties(" size=\"20\"");
        getFields().add(historyLast);

        historyCurrent = new Field();
        historyCurrent.setName("historyCurrent");
        historyCurrent.setCaption(" saldo");
        historyCurrent.setFormater(MoneyFormatter.getInstance());
        historyCurrent.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        historyCurrent.setInputProperties(" size=\"20\"");
        getFields().add(historyCurrent);

        historyDescription = new Field();
        historyDescription.setName("historyDescription");
        historyDescription.setCaption(" description");
        //revisionDescription.setInfo("revision description");
        historyDescription.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        historyDescription.setInputProperties(" size=\"20\"");
        getFields().add(historyDescription);

        historyTimestamp = new Field();
        historyTimestamp.setName("historyTimestamp");
        historyTimestamp.setCaption("timestamp");
        //historyTimestamp.setInfo("history timestamp");
        historyTimestamp.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        historyTimestamp.setInputProperties(" size=\"12\"");
        //historyTimestamp.setFormatter(DateFormatter.getInstanceDateTime(this));
        getFields().add(historyTimestamp);

        historyUserId = new Field();
        historyUserId.setName("historyUserId");
        historyUserId.setCaption("user");
        //historyUserId.setInfo("history user id");
        historyUserId.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        historyUserId.setInputProperties(" size=\"20\"");
        getFields().add(historyUserId);

        historyUnitId = new Field();
        historyUnitId.setName("historyUnitId");
        historyUnitId.setCaption("unit");
        //historyUnitId.setInfo("history unit id");
        historyUnitId.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        historyUnitId.setInputProperties(" size=\"20\"");
        getFields().add(historyUnitId);

        historyEmployeeId = new Field();
        historyEmployeeId.setName("historyEmployeeId");
        historyEmployeeId.setCaption("employee");
        //historyEmployeeId.setInfo("history employee id");
        historyEmployeeId.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        historyEmployeeId.setInputProperties(" size=\"20\"");
        getFields().add(historyEmployeeId);


        fillFields();
        getFieldsList().clear();
        getFieldsList().add(historyDate);
        getFieldsList().add(historyEmployeeId);
        getFieldsList().add(historyPlatform);
        getFieldsList().add(historyType);
        getFieldsList().add(historyNumber);
        getFieldsList().add(historyUnitId);
        getFieldsList().add(historyStatus);
        getFieldsList().add(historyLast);
        getFieldsList().add(historyValue);
        getFieldsList().add(historyCurrent);
//        getFieldsList().add(historyTimestamp);
        getFieldsList().add(historyDescription);

        getFieldsView().clear();
        getFieldsView().add(historyEmployeeId);
        getFieldsView().add(historyPlatform);
        getFieldsView().add(historyUserId);
        getFieldsView().add(historyUnitId);
        getFieldsView().add(historyStatus);
        getFieldsView().add(historyLast);
        getFieldsView().add(historyValue);
        getFieldsView().add(historyCurrent);
        getFieldsView().add(historyTimestamp);
        getFieldsView().add(historyDescription);

        getActions().add(actionExport);
        getActions().remove(Action.ADD);
        getActions().remove(Action.DELETE);
        getActions().remove(Action.DELETE_MULTI);
        getActions().remove(Action.EDIT);
        getActions().remove(Action.VIEW);
        getActions().remove(Action.PRINT);
        getActions().remove(Action.PRINT_ALL);
        getActions().remove(Action.PRINT_MULTI);

//        setExportList(getClass().getResourceAsStream("HisEmployeeList.jasper"));
//        setExport(getClass().getResourceAsStream("HisEmployee.jasper"));
        getController().setOrder(historyTimestamp);
        getController().setOrderType(Controller.ORDER_DESC);
    }

//       @Override
//    public void beforeExport() {
//        getExportParams().put("title", Dictionary.getValue(this, "History Employee"));
//    }
//
//    @Override
//    public void beforeExportMulti() {
//        beforeExport();
//    }
    @Override
    public boolean isSearchVisible() {
        return false;
    }

    @Override
    public boolean isTitleVisible() {
        return false;
    }

    @Override
    public StringBuffer process() {
        if (getAction().equals(actionExport)) {
            try {
                updateExportParams();
                if ("2".equals(getCurrentUser().getUserExport())) {
                    getResponse().setHeader("Content-Type", "application/vnd.ms-excel ");
                } else {
                    getResponse().setHeader("Content-Type", "application/pdf");
                }
                JRDataSource datasource = null;
                EntityManager em = getController().getEntityManager();

                String mode = getRequest().getParameter("mode");

                MasterUnit unit = null;
                String filterUnit = Utility.getParameterEmpty(getRequest(), "unit");
                System.out.println("UNIT-->" + filterUnit);
                try {
                    unit = (MasterUnit) getEntityManager().createQuery("SELECT t FROM MasterUnit t WHERE t.unitName='" + filterUnit + "' ").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
                    getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit") + " - " + unit.getUnitName());
                } catch (Exception e) {
                    getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit"));
                }

                GapMasterEmployee employee = null;
                String filterEmployee = Utility.getParameterEmpty(getRequest(), "employee");
                System.out.println("EMP-->" + filterEmployee);
                try {
                    employee = (GapMasterEmployee) getEntityManager().createQuery("SELECT t FROM GapMasterEmployee t WHERE t.employeeNameFirst='" + filterEmployee + "' ").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
                    getExportParams().put("employee", Utility.getParameterEmpty(getRequest(), "employee") + " - " + employee.getEmployeeNameFirst());
                } catch (Exception e) {
                    getExportParams().put("employee", Utility.getParameterEmpty(getRequest(), "employee"));
                }

                if ("1".equals(mode)) {
                    String filterDate = Utility.getParameterEmpty(getRequest(), "recapDate");
                    getExportParams().put("filter", Dictionary.getValue(this, "From") + " " + Utility.getPostParameterEmpty(this, "recapDate"));
                    getExportParams().put("title", Dictionary.getValue(this, "History Employee") + " #" + Dictionary.getValue(this, "Daily"));
                    getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit"));
                    getExportParams().put("employee", Utility.getParameterEmpty(getRequest(), "employee"));
                    if (!"".equals(filterDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "Date") + " " + Utility.getPostParameterEmpty(this, "recapDate"));
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));

                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                            getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));

                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));
                        } else {
                            getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyUnitId.unitName='" + filterUnit + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));
                        }
                        getController().setPaginationFilter("&mode=" + mode + "&unit=" + filterUnit + "&employee=" + filterEmployee + "&recapDate=" + filterDate);
                    }

                } else if ("2".equals(mode)) {
                    String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                    if (!"".equals(filterStartDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "From") + " " + Utility.getPostParameterEmpty(this, "startDate"));
                        getExportParams().put("title", Dictionary.getValue(this, "History Employee") + " #" + Dictionary.getValue(this, "Weekly"));
                        getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit"));
                        getExportParams().put("employee", Utility.getParameterEmpty(getRequest(), "employee"));
                        DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                        Calendar cal = Calendar.getInstance();
                        cal.setTime((Date) dateFormatter.parse(filterStartDate));
                        String start = dateFormatter.format(cal.getTime());
                        cal.add(Calendar.DATE, 7);
                        String end = dateFormatter.format(cal.getTime());
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                            getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        } else {
                            getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyUnitId.unitName='" + filterUnit + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                        }

                        getController().setPaginationFilter("&mode=" + mode + "&unit=" + filterUnit + "&employee=" + filterEmployee + "&startDate=" + filterStartDate);
                    }

                } else if ("3".equals(mode)) {
                    String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                    String filterMonth = Utility.getParameterEmpty(getRequest(), "recapMonth");
                    if (!"".equals(filterYear) && !"".equals(filterMonth)) {
                        getExportParams().put("filter", Utility.getPostParameterEmpty(this, "recapYear") + " - " + Dictionary.getValue(this, Utility.getLongMonthName(Integer.parseInt(Utility.getPostParameterEmpty(this, "recapMonth")))));
                        getExportParams().put("title", Dictionary.getValue(this, "History Payment") + " #" + Dictionary.getValue(this, "Monthly"));
                        getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit"));
                        getExportParams().put("employee", Utility.getParameterEmpty(getRequest(), "employee"));
                        DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                        StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", filterMonth);
                        Utility.replaceAll(date, "dd", "01");
                        String start = date.toString();
                        Calendar cal = Calendar.getInstance();
                        cal.setTime((Date) dateFormatter.parse(start));
                        int max = cal.getActualMaximum(Calendar.DATE);
                        date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", filterMonth);
                        Utility.replaceAll(date, "dd", "" + max);
                        String end = date.toString();
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                            getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                        } else {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyEmployeeId.employeeName='" + filterEmployee + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                        }

                        getController().setPaginationFilter("&mode=" + mode + "&unit=" + filterUnit + "&employee=" + filterEmployee + "&recapYear=" + filterYear + "&filterMonth=" + filterMonth);
                    }

                } else if ("4".equals(mode)) {
                    String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                    if (!"".equals(filterYear)) {

                        getExportParams().put("filter", Dictionary.getValue(this, "Year") + " " + Utility.getPostParameterEmpty(this, "recapYear"));
                        getExportParams().put("title", Dictionary.getValue(this, "History Payment") + " #" + Dictionary.getValue(this, "Yearly"));
                        getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit"));
                        getExportParams().put("employee", Utility.getParameterEmpty(getRequest(), "employee"));
                        DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                        StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", "01");
                        Utility.replaceAll(date, "dd", "01");
                        String start = date.toString();
                        date = new StringBuffer(dateFormatter.getFormat());
                        Utility.replaceAll(date, "yyyy", filterYear);
                        Utility.replaceAll(date, "MM", "12");
                        Utility.replaceAll(date, "dd", "31");
                        String end = date.toString();
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                            getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                        } else {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                        }

                        getController().setPaginationFilter("&mode=" + mode + "&unit=" + filterUnit + "&employee=" + filterEmployee + "&recapYear=" + filterYear);

                    }

                } else if ("5".equals(mode)) {
                    String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                    String filterEndDate = Utility.getParameterEmpty(getRequest(), "endDate");
                    getExportParams().put("title", Dictionary.getValue(this, "History Employee") + " #" + Dictionary.getValue(this, "Custom"));
                    if (!"".equals(filterStartDate) && "".equals(filterEndDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "From") + " " + Utility.getPostParameterEmpty(this, "startDate"));
                        getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit"));
                        getExportParams().put("employee", Utility.getParameterEmpty(getRequest(), "employee"));
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyDate >= :start");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                            getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyDate >= :start");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyDate >= :start");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        } else {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyDate >= :start");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        }
                    } else if ("".equals(filterStartDate) && !"".equals(filterEndDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "To") + " " + Utility.getPostParameterEmpty(this, "endDate"));
                        getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit"));
                        getExportParams().put("employee", Utility.getParameterEmpty(getRequest(), "employee"));
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyDate <= :end");
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                            getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyDate <= :end");
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyDate <= :end");
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        } else {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyEmployeeId.employeeName='" + filterEmployee + "' AND t.historyDate <= :end");
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        }

                    } else if (!"".equals(filterStartDate) && !"".equals(filterEndDate)) {
                        getExportParams().put("filter", Dictionary.getValue(this, "From") + " " + Utility.getPostParameterEmpty(this, "startDate") + " " + Dictionary.getValue(this, "to") + " " + Utility.getPostParameterEmpty(this, "endDate"));
                        getExportParams().put("unit", Utility.getParameterEmpty(getRequest(), "unit"));
                        getExportParams().put("employee", Utility.getParameterEmpty(getRequest(), "employee"));
                        DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                        if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                            getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        } else {
                            getController().setWhere("t.historyUnitId.unitName='" + filterUnit + "' AND t.historyEmployeeId.employeeNameFirst='" + filterEmployee + "' AND t.historyDate BETWEEN :start AND :end");
                            getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                            getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                        }
                    }
                }

                datasource = new JRBeanArrayDataSource(getController().getData(true).toArray());
                JasperPrint jasperPrint = JasperFillManager.fillReport(getClass().getResourceAsStream("MasHisEmployeeList.jasper"), getExportParams(), datasource);
                if ("2".equals(getCurrentUser().getUserExport())) {
                    JRXlsExporter excelExporter = new JRXlsExporter();
                    excelExporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
                    excelExporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, getResponse().getOutputStream());
                    excelExporter.exportReport();
                } else {
                    JasperExportManager.exportReportToPdfStream(jasperPrint, getResponse().getOutputStream());
                }
                return null;
            } catch (Exception e) {
                Logger.getInstance().log(e);
            }
            return new StringBuffer();
        } else {
            StringBuffer content = Utility.load(Bynga.BYNGA_PACKAGE_THEME + "content.html");
            Template template = new Template();
            template.loadTemplate("/com/gap/historyemployee/recap.html");

            template.addValue(MESSAGE_DAILY, Dictionary.getValue(this, "Please enter date"));
            template.addValue(MESSAGE_WEEKLY, Dictionary.getValue(this, "Please enter start date"));
            template.addValue(MESSAGE_MONTHLY_YEAR, Dictionary.getValue(this, "Please select year"));
            template.addValue(MESSAGE_MONTHLY_MONTH, Dictionary.getValue(this, "Please select month"));
            template.addValue(MESSAGE_YEARLY, Dictionary.getValue(this, "Please select year"));
            template.addValue(MESSAGE_CUSTOM, Dictionary.getValue(this, "Please enter start date or end date or both"));

            EntityManager em = getEntityManager();

            template.addValue(SELECT_EMPLOYEE, Dictionary.getValue(this, "Select Employee"));
            String selectEmployee = "<option value=\"*\">" + Dictionary.getValue(this, "All Employee") + "</option>";
            String employee = Utility.getParameterEmpty(getRequest(), "employee");
            try {
                List<GapMasterEmployee> employees = getEntityManager().createQuery("SELECT t FROM GapMasterEmployee t  WHERE t.employeeActive='1'").setHint(Persistence.REFRESH_HINT, true).getResultList();
                for (GapMasterEmployee emp : employees) {
                    selectEmployee += "<option value=\"" + emp.getEmployeeNameFirst() + "\""
                            + (emp.getEmployeeNameFirst().equals(employee) ? " selected" : "") + ">"
                            + emp.getEmployeeNumber() + " - " + emp.getEmployeeNameFirst() + "</option>";
                }
            } catch (Exception e) {
            }
            template.addValue(SELECT_EMPLOYEE_VALUES, selectEmployee);
//--------------------
            template.addValue(SELECT_UNIT, Dictionary.getValue(this, "Select Unit"));
            String selectUnit = "<option value=\"*\">" + Dictionary.getValue(this, "All Unit") + "</option>";
            String unit = Utility.getParameterEmpty(getRequest(), "unit");
            try {
                List<MasterUnit> units = getEntityManager().createQuery("SELECT t FROM MasterUnit t ORDER BY t.unitName ").setHint(Persistence.REFRESH_HINT, true).getResultList();
                for (MasterUnit unt : units) {
                    selectUnit += "<option value=\"" + unt.getUnitName() + "\"" + (unt.getUnitName().equals(unit) ? " selected" : "") + ">" + unt.getUnitName() + "</option>";
                }
            } catch (Exception e) {
            }
            template.addValue(SELECT_UNIT_VALUES, selectUnit);
//----------------------------------------
            System.out.println("UNITTTTTT-->" + unit);
            System.out.println("EMPLOYYYY-->" + employee);

            template.addValue(SELECT_MODE, Dictionary.getValue(this, "Select mode"));
            template.addValue(SELECT_MODE_ONCHANGE, Dictionary.getValue(this, "'?m=" + getMenu().getMenuName() + "&a=" + getMenu().getMenuAction() + "&mode='+this.value"));

            String selectMode = "";
            String mode = Utility.getParameterEmpty(getRequest(), "mode");
            if ("".equals(mode)) {
                mode = "1";
            }
            selectMode += "<option value=\"1\"" + ("1".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Daily") + "</option>";
            selectMode += "<option value=\"2\"" + ("2".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Weekly") + "</option>";
            selectMode += "<option value=\"3\"" + ("3".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Monthly") + "</option>";
            selectMode += "<option value=\"4\"" + ("4".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Yearly") + "</option>";
            selectMode += "<option value=\"5\"" + ("5".equals(mode) ? " selected" : "") + ">" + Dictionary.getValue(this, "Custom") + "</option>";
            template.addValue(SELECT_VALUES, selectMode);

            boolean complete = false;
            if ("1".equals(mode)) {
                StringBuffer parameter = template.getEmptyElement(MODE_DAILY);
                Utility.replaceAll(parameter, DAILY_SELECT_DATE, Dictionary.getValue(this, "Date"));
                Utility.replaceAll(parameter, DAILY_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "recapDate"));
                template.addElement(MODE_DAILY, parameter);
                template.addElement(MODE_WEEKLY, null);
                template.addElement(MODE_MONTHLY, null);
                template.addElement(MODE_YEARLY, null);
                template.addElement(MODE_CUSTOM, null);
                String filterDate = Utility.getParameterEmpty(getRequest(), "recapDate");
                if (!"".equals(filterDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&unit=" + Utility.getPostParameterEmpty(this, "unit") + "&employee=" + Utility.getPostParameterEmpty(this, "employee") + "&recapDate=" + Utility.getPostParameterEmpty(this, "recapDate"));
                    template.addElement(RECAP_EXPORT, export);
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));

                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));

                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyUnitId.unitName='" + unit + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));
                    } else {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyUnitId.unitName='" + unit + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterDate + " 23:59:59"));
                    }
                    getController().setPaginationFilter("&mode=" + mode + "&unit=" + unit + "&employee=" + employee + "&recapDate=" + filterDate);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }

            } else if ("2".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                StringBuffer parameter = template.getEmptyElement(MODE_WEEKLY);
                Utility.replaceAll(parameter, WEEKLY_SELECT_START_DATE, Dictionary.getValue(this, "Start date"));
                Utility.replaceAll(parameter, WEEKLY_START_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "startDate"));
                template.addElement(MODE_WEEKLY, parameter);
                template.addElement(MODE_MONTHLY, null);
                template.addElement(MODE_YEARLY, null);
                template.addElement(MODE_CUSTOM, null);
                String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                if (!"".equals(filterStartDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&unit=" + Utility.getPostParameterEmpty(this, "unit") + "&employee=" + Utility.getPostParameterEmpty(this, "employee") + "&startDate=" + Utility.getParameterEmpty(getRequest(), "startDate"));
                    template.addElement(RECAP_EXPORT, export);

                    DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime((Date) dateFormatter.parse(filterStartDate));
                    String start = dateFormatter.format(cal.getTime());
                    cal.add(Calendar.DATE, 7);
                    String end = dateFormatter.format(cal.getTime());
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyUnitId.unitName='" + unit + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    } else {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyUnitId.unitName='" + unit + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    }
                    getController().setPaginationFilter("&mode=" + mode + "&unit=" + unit + "&employee=" + employee + "&startDate=" + filterStartDate);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }
            } else if ("3".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                template.addElement(MODE_WEEKLY, null);
                StringBuffer parameter = template.getEmptyElement(MODE_MONTHLY);
                Utility.replaceAll(parameter, MONTHLY_SELECT_YEAR, Dictionary.getValue(this, "Year"));
                String selectYear = "<option value=\"\">" + Dictionary.getValue(this, "Select year") + "</option>";
                int year = Calendar.getInstance().get(Calendar.YEAR);
                for (int i = year; i >= 2010; i--) {
                    selectYear += "<option value=\"" + i + "\"" + (("" + i).equals(Utility.getParameterEmpty(getRequest(), "recapYear")) ? " selected" : "") + ">" + i + "</option>";
                }
                Utility.replaceAll(parameter, MONTHLY_SELECT_YEAR_VALUES, selectYear);
                Utility.replaceAll(parameter, MONTHLY_SELECT_MONTH, Dictionary.getValue(this, "Month"));
                String selectMonth = "<option value=\"\">" + Dictionary.getValue(this, "Select month") + "</option>";
                for (int i = 1; i <= 12; i++) {
                    selectMonth += "<option value=\"" + i + "\"" + (("" + i).equals(Utility.getParameterEmpty(getRequest(), "recapMonth")) ? " selected" : "") + ">" + Dictionary.getValue(this, Utility.getLongMonthName(i)) + "</option>";
                }
                Utility.replaceAll(parameter, MONTHLY_SELECT_MONTH_VALUES, selectMonth);
                template.addElement(MODE_MONTHLY, parameter);
                template.addElement(MODE_YEARLY, null);
                template.addElement(MODE_CUSTOM, null);
                String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                String filterMonth = Utility.getParameterEmpty(getRequest(), "recapMonth");
                if (!"".equals(filterYear) && !"".equals(filterMonth)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&unit=" + Utility.getPostParameterEmpty(this, "unit") + "&employee=" + Utility.getPostParameterEmpty(this, "employee") + "&recapYear=" + Utility.getPostParameterEmpty(this, "recapYear") + "&recapMonth=" + Utility.getPostParameterEmpty(this, "recapMonth"));
                    template.addElement(RECAP_EXPORT, export);

                    DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                    StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", filterMonth);
                    Utility.replaceAll(date, "dd", "01");
                    String start = date.toString();
                    Calendar cal = Calendar.getInstance();
                    cal.setTime((Date) dateFormatter.parse(start));
                    int max = cal.getActualMaximum(Calendar.DATE);
                    date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", filterMonth);
                    Utility.replaceAll(date, "dd", "" + max);
                    String end = date.toString();
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyUnitId.unitName='" + unit + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    } else {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyUnitId.unitName='" + unit + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    }
                    getController().setPaginationFilter("&mode=" + mode + "&unit=" + unit + "&employee=" + employee + "&recapYear=" + filterYear + "&filterMonth=" + filterMonth);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }
            } else if ("4".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                template.addElement(MODE_WEEKLY, null);
                template.addElement(MODE_MONTHLY, null);
                StringBuffer parameter = template.getEmptyElement(MODE_YEARLY);
                Utility.replaceAll(parameter, YEARLY_SELECT_YEAR, Dictionary.getValue(this, "Year"));
                String selectYear = "<option value=\"\">" + Dictionary.getValue(this, "Select year") + "</option>";
                int year = Calendar.getInstance().get(Calendar.YEAR);
                for (int i = year; i >= 2010; i--) {
                    selectYear += "<option value=\"" + i + "\"" + (("" + i).equals(Utility.getParameterEmpty(getRequest(), "recapYear")) ? " selected" : "") + ">" + i + "</option>";
                }
                Utility.replaceAll(parameter, YEARLY_SELECT_YEAR_VALUES, selectYear);
                template.addElement(MODE_YEARLY, parameter);
                template.addElement(MODE_CUSTOM, null);
                String filterYear = Utility.getParameterEmpty(getRequest(), "recapYear");
                if (!"".equals(filterYear)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&unit=" + Utility.getPostParameterEmpty(this, "unit") + "&employee=" + Utility.getPostParameterEmpty(this, "employee") + "&recapYear=" + Utility.getPostParameterEmpty(this, "recapYear"));
                    template.addElement(RECAP_EXPORT, export);

                    DateFormatter dateFormatter = DateFormatter.getInstanceDate(this);
                    StringBuffer date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", "01");
                    Utility.replaceAll(date, "dd", "01");
                    String start = date.toString();
                    date = new StringBuffer(dateFormatter.getFormat());
                    Utility.replaceAll(date, "yyyy", filterYear);
                    Utility.replaceAll(date, "MM", "12");
                    Utility.replaceAll(date, "dd", "31");
                    String end = date.toString();
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));

                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyUnitId.unitName='" + unit + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    } else {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyUnitId.unitName='" + unit + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(start + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(end + " 23:59:59"));
                    }
                    getController().setPaginationFilter("&mode=" + mode + "&unit=" + unit + "&employee=" + employee + "&recapYear=" + filterYear);
                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }
            } else if ("5".equals(mode)) {
                template.addElement(MODE_DAILY, null);
                template.addElement(MODE_WEEKLY, null);
                template.addElement(MODE_MONTHLY, null);
                template.addElement(MODE_YEARLY, null);
                String filterStartDate = Utility.getParameterEmpty(getRequest(), "startDate");
                String filterEndDate = Utility.getParameterEmpty(getRequest(), "endDate");
                if (!"".equals(filterStartDate) && "".equals(filterEndDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&unit=" + Utility.getPostParameterEmpty(this, "unit") + "&employee=" + Utility.getPostParameterEmpty(this, "employee") + "&startDate=" + Utility.getPostParameterEmpty(this, "startDate") + "&endDate=" + Utility.getPostParameterEmpty(this, "endDate"));
                    template.addElement(RECAP_EXPORT, export);

                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyDate >= :start");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyDate >= :start");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyUnitId.unitName='" + unit + "' AND t.historyDate >= :start");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                    } else {
                        getController().setWhere("t.historyUnitId.unitName='" + unit + "' AND t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyDate >= :start");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                    }
                    complete = true;
                } else if ("".equals(filterStartDate) && !"".equals(filterEndDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&unit=" + Utility.getPostParameterEmpty(this, "unit") + "&employee=" + Utility.getPostParameterEmpty(this, "employee") + "&endDate=" + Utility.getPostParameterEmpty(this, "endDate"));
                    template.addElement(RECAP_EXPORT, export);
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyDate <= :end");
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyDate <= :end");
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyUnitId.unitName='" + unit + "' AND t.historyDate <= :end");
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    } else {
                        getController().setWhere("t.historyUnitId.unitName='" + unit + "' AND t.historyEmployeeId.employeeNameFirst='" + employee + "' AND  t.historyDate <= :end");
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    }
                    complete = true;
                } else if (!"".equals(filterStartDate) && !"".equals(filterEndDate)) {
                    StringBuffer export = template.getEmptyElement(RECAP_EXPORT);
                    Utility.replaceAll(export, RECAP_EXPORT_LINK, "?m=" + getMenu().getMenuName() + "&a=" + actionExport.getName() + "&mode=" + Utility.getPostParameterEmpty(this, "mode") + "&unit=" + Utility.getPostParameterEmpty(this, "unit") + "&employee=" + Utility.getPostParameterEmpty(this, "employee") + "&startDate=" + Utility.getPostParameterEmpty(this, "startDate") + "&endDate=" + Utility.getPostParameterEmpty(this, "endDate"));
                    template.addElement(RECAP_EXPORT, export);
                    DateFormatter dateTimeFormatter = DateFormatter.getInstanceDateTime(this);
                    if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit")) && "*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "unit"))) {
                        getController().setWhere("t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    } else if ("*".equals(Utility.getParameterEmpty(getRequest(), "employee"))) {
                        getController().setWhere("t.historyUnitId.unitName='" + unit + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    } else {
                        getController().setWhere("t.historyUnitId.unitName='" + unit + "' AND t.historyEmployeeId.employeeNameFirst='" + employee + "' AND t.historyDate BETWEEN :start AND :end");
                        getController().addParameter("start", dateTimeFormatter.parse(filterStartDate + " 00:00:00"));
                        getController().addParameter("end", dateTimeFormatter.parse(filterEndDate + " 23:59:59"));
                    }

                    complete = true;
                } else {
                    template.addValue(template.getEmptyElement(RECAP_EXPORT).toString(), null);
                }
                getController().setPaginationFilter("&mode=" + mode + "&unit=" + unit + "&startDate=" + filterStartDate + "&endDate=" + filterEndDate);
                StringBuffer parameter = template.getEmptyElement(MODE_CUSTOM);
                Utility.replaceAll(parameter, CUSTOM_SELECT_START_DATE, Dictionary.getValue(this, "Start date"));
                Utility.replaceAll(parameter, CUSTOM_START_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "startDate"));
                Utility.replaceAll(parameter, CUSTOM_SELECT_END_DATE, Dictionary.getValue(this, "End date"));
                Utility.replaceAll(parameter, CUSTOM_END_DATE_VALUE, Utility.getParameterEmpty(getRequest(), "endDate"));
                template.addElement(MODE_CUSTOM, parameter);
            }
            template.addValue(BUTTON_SEARCH, Dictionary.getValue(this, "Search"));
            int record = getController().getRecordCount();
            template.addValue(RECORD, Dictionary.getValue(this, "Total all record") + ": " + NumberFormatter.getInstance().format(record));
            if (record > 10000000) {
                template.addValue(WARNING, "return confirm('" + Dictionary.getValue(this, "Records is to big need long time to process, approximately") + " " + NumberFormatter.getInstance().format(record) + " " + Dictionary.getValue(this, "records, are you sure want to continue?") + "');");
            } else {
                template.addValue(WARNING, "");
            }
            if (complete) {
                template.addValue(LIST_DATA, PageList.process(this).toString());
            } else {
                template.addValue(LIST_DATA, "");
            }
            Utility.replaceAll(content, Template.CONTENT_CONTENT, template.process().toString());
            updateContent(content, getMenu(), true);
            return content;
        }
    }
}
